<?php
namespace app\admin\controller;
use think\Db;
use think\Request;
use think\Controller;
class Users extends Common{
    //会员列表
    public function index(){
        if(request()->isPost()){
            $key=input('post.key');
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $list=db('users')->alias('u')
                ->join(config('database.prefix').'user_level ul','u.level = ul.level_id','left')
                ->field('u.*,ul.level_name')
                ->where('u.mobile|u.username','like',"%".$key."%")
                ->order('u.id desc')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();
            foreach ($list['data'] as $k=>$v){
				$username = db('users')->where('id', $v['parent_id'])->value('username');
                $list['data'][$k]['parent'] = $username;
				$list['data'][$k]['last_login'] = $list['data'][$k]['last_login']=='0000-00-00 00:00:00'?"":$list['data'][$k]['last_login'];
            }
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
        return $this->fetch();
    }
	
	 public function profile(){
        if(request()->isPost()){
            $key=input('post.key');
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $list=db('log')->alias('u')
                ->join(config('database.prefix').'users ul','u.m_id = ul.id','left')
                ->field('u.*,ul.username')
                ->where('u.m_id|ul.username','like',"%".$key."%")
				->where('u.type', 3)
                ->order('u.id desc')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
        return $this->fetch();
    }
	
	public function signlog(){
        if(request()->isPost()){
            $key=input('post.key');
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $list=db('signlog')->alias('u')
                ->join(config('database.prefix').'users ul','u.m_id = ul.id','left')
                ->field('u.*,ul.username')
                ->where('u.m_id|ul.username','like',"%".$key."%")
                ->order('u.id desc')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
        return $this->fetch();
    }
	
	public function withdraw(){
        if(request()->isPost()){
            $key=input('post.key');
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $list=db('withdraw')->alias('u')
                ->join(config('database.prefix').'users ul','u.m_id = ul.id','left')
                ->field('u.*,ul.username')
                ->where('u.m_id|ul.username','like',"%".$key."%")
				
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
        return $this->fetch();
    }
	
	
    public function usersState(){
        $id=input('post.id');
        $status=db('users')->where(array('id'=>$id))->value('is_lock');//判断当前状态情况
        if($status==1){
            $data['is_lock'] = 0;
            db('users')->where(array('id'=>$id))->setField($data);
            $result['is_lock'] = 0;
            $result['status'] = 1;
        }else{
            $data['is_lock'] = 1;
            db('users')->where(array('id'=>$id))->setField($data);
            $result['is_lock'] = 1;
            $result['status'] = 1;
        }
        return $result;
    }

    public function edit($id=''){
        if(request()->isPost()){
            $user = db('users');
            $data = input('post.');
           
            if(empty($data['password'])){
                unset($data['password']);
            }else{
                $data['password'] = md5($data['password']);
            }
            if ($user->update($data)!==false) {
                $result['msg'] = '会员修改成功!';
                $result['url'] = url('index');
                $result['code'] = 1;
            } else {
                $result['msg'] = '会员修改失败!';
                $result['code'] = 0;
            }
            return $result;
        }else{
            $user_level=db('upgrade')->select();
            $info = db('users')->where("id", $id)->find();
            $this->assign('info',json_encode($info,true));
            $this->assign('user',$info);
            $this->assign('title',lang('edit').lang('user'));
            $this->assign('user_level', $user_level);

            return $this->fetch();
        }
    }
	
	
	public function payadd($id=''){
        if(request()->isPost()){
            $data = input('post.');
			$user = db('users')->where("id", $data['m_id'])->find();
		    
			if($user){
				$data['add_time'] = date("Y-m-d H:i:s");
				if (db("onlinepay")->insert($data)!==false) {
					db('users')->where("id", $data['m_id'])->setInc('money', $data['fee']);
					$result['msg'] = '会员充值成功!';
					$result['url'] = url('onlinepay');
					$result['code'] = 1;
				} else {
					$result['msg'] = '会员充值失败!';
					$result['code'] = 0;
				}
			}else{
				$result['msg'] = '不存在的会员ID!';
				$result['code'] = 0;
			}
            return $result;
        }else{
            return $this->fetch();
        }
    }
	

	public function onlinepay(){
		if(request()->isPost()){
            $key=input('post.key');
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $list=db('onlinepay')->alias('u')
                ->join(config('database.prefix').'users ul','u.m_id = ul.id','left')
                ->field('u.*,ul.username')
                ->where('u.id|ul.username','like',"%".$key."%")
                ->order('u.id desc')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
        return $this->fetch();
	}
	
	public function payDel($id=""){
		db("withdraw")->where("id", $id)->delete();
        return $result = ['code'=>1,'msg'=>'删除成功!'];
	}
	
	
	
	public function signlog_del($id=""){
		db("signlog")->where("id", $id)->delete();
        return $result = ['code'=>1,'msg'=>'删除成功!'];
	}
	
	
	public function profileDel($id=""){
		db("log")->where("id", $id)->delete();
        return $result = ['code'=>1,'msg'=>'删除成功!'];
	}
	
    public function usersDel($user_id=''){
        db("users")->where("id", $user_id)->delete();
        return $result = ['code'=>1,'msg'=>'删除成功!'];
    }
	
	
	public function onlinepaydelall(){
        $map['id'] =array('in',input('param.ids/a'));
        db('onlinepay')->where($map)->delete();

        $result['msg'] = '删除成功！';
        $result['code'] = 1;
        $result['url'] = url('onlinepay');
        return $result;
    }
	
    public function delall(){
        $map['id'] =array('in',input('param.ids/a'));
        db('users')->where($map)->delete();

        $result['msg'] = '删除成功！';
        $result['code'] = 1;
        $result['url'] = url('index');
        return $result;
    }
	
	public function pass(){
		$id = input('param.id');
		$ret = db("withdraw")->where('id', $id)->update(['status'=>1]);
		if($ret){
			echo 0;
		}else{
			echo 1;
		}
	}
	
	public function refuse(){
		$id = input('param.id');
		$reason = input('param.reason');
		
		Db::startTrans();
		try{
			$ret = db("withdraw")->where('id', $id)->update(['status'=>2, 'reason'=>$reason]);
			$row = db("withdraw")->where('id', $id)->find();
			db('users')->where('id', $row['m_id'])->setInc('coin', $row['count']);
			echo 0;
			Db::commit();
		}catch(\Exception $e){
			echo 1;
			Db::rollback();
		}
	}
	

    /***********************************会员组***********************************/
    public function userGroup(){
        if(request()->isPost()){
            $userLevel=db('user_level');
            $list=$userLevel->order('sort')->select();
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list,'rel'=>1];
        }
        return $this->fetch();
    }
    public function groupAdd(){
        if(request()->isPost()){
            $data = input('post.');
            $data['open'] = input('post.open') ? input('post.open') : 0;
            db('user_level')->insert($data);
            $result['msg'] = '会员组添加成功!';
            $result['url'] = url('userGroup');
            $result['code'] = 1;
            return $result;
        }else{
            $this->assign('title',lang('add')."会员组");
            $this->assign('info','null');
            return $this->fetch('groupForm');
        }
    }
    public function groupEdit(){
        if(request()->isPost()) {
            $data = input('post.');
            db('user_level')->update($data);
            $result['msg'] = '会员组修改成功!';
            $result['url'] = url('userGroup');
            $result['code'] = 1;
            return $result;
        }else{
            $map['level_id'] = input('param.level_id');
            $info = db('user_level')->where($map)->find();
            $this->assign('title',lang('edit')."会员组");
            $this->assign('info',json_encode($info,true));
            return $this->fetch('groupForm');
        }
    }
    public function groupDel(){
        $level_id=input('level_id');
        if (empty($level_id)){
            return ['code'=>0,'msg'=>'会员组ID不存在！'];
        }
        db('user_level')->where(array('level_id'=>$level_id))->delete();
        return ['code'=>1,'msg'=>'删除成功！'];
    }
    public function groupOrder(){
        $userLevel=db('user_level');
        $data = input('post.');
        $userLevel->update($data);
        $result['msg'] = '排序更新成功!';
        $result['url'] = url('userGroup');
        $result['code'] = 1;
        return $result;
    }

    public function mtree(){
		$tree = $this->getSon("");
		$this->assign('tree', json_encode($tree, true));
		return view();
	}
	
	public function getSon($id){
		$tmp = [];
		$rows = db('users')->WHERE('parent_id', $id)->select();
		foreach($rows as $itm){
			$tmp[] = ['key'=>$itm['id'], 'name'=>$itm['username'], 'parent'=>$id, 'id'=>$itm['id']];
			$is_next = db('users')->where('parent_id', $itm['id'])->count();
			if($is_next){
				$ret = $this->getSon($itm['id']);
				$tmp = array_merge($tmp, $ret);
			}
		}
		return $tmp;
	}


}